

import GameManager from "../game/GameManager";
import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import Helper from "../libs/HelperMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UIRank extends cc.Component {


    @property(cc.Prefab)
    item: cc.Prefab = null;

    @property(cc.Node)
    rankContent: cc.Node = null;

    @property(cc.Node)
    myRank: cc.Node = null;

    @property(cc.Node)
    btnLocal: cc.Node = null;

    @property(cc.Node)
    btnWorld: cc.Node = null;

    private isLocal: boolean = true;
    private curdata: Array<number>  = new Array

    onLoad() { }

    start() {
        this.refreshUI();
        this.refreshList();
        this.refreshMyRank();
    }

   

    refreshUI(){
        if(cc.director.getScene().name == "game" && !GameData.instance.gameModel){
            this.btnWorld.active = false;
        }else{
            this.btnWorld.active = true;
        }
    }

    refreshList() {
        if(this.isLocal){
            if(cc.director.getScene().name == "index"){
                this.curdata = GameData.instance.gameModel ? GameData.instance.playScores : GameData.instance.playCrazyScores;
            }else{
                this.curdata = GameData.instance.playScores
            }
        }else{
            this.curdata = Constants.WorldScore;
        }
        this.rankContent.destroyAllChildren();
        this.loadByFrame(this.curdata.length,10);
    }

    /**
    * 分帧加载
    * total ：总共需要加载的数量
    * part :每帧加载的数量
    */
    loadByFrame(total:number,part:number) {
        let index = 0;  // 当前下标
        // 加载函数
        const load = () => {
            // 加载 item
            const count = Math.min(total - index, part);
            for (let i = 0; i < count; i++) {
                this.addItem(index);
                index++;
            }
            // 是否还有
            if (index < total) {
                // 下一帧继续加载
                this.scheduleOnce(() => load());
            }
        }
        // 开始加载
        load();
    }


     /**
     * 添加 item
     * @param index 下标
     */
    async addItem(index: number) {
        let item: cc.Node = cc.instantiate(this.item);
        item.setParent(this.rankContent)
        let img = item.getChildByName("img").getComponent(cc.Sprite);
        /**不要async时用这个方法加载图片 */
        // if(i<3){
        //     // cc.loader.loadRes(`image/${i+1}`, cc.SpriteFrame, function (err, spriteFrame) {
        //     //     img.spriteFrame = spriteFrame;
        //     // });
        // }
        img.node.active = index < 3;
        img.node.active && (img.spriteFrame = await Helper.loadRes(`image/rank${index + 1}`, cc.SpriteFrame))

        let rank = item.getChildByName("rank").getComponent(cc.Label);
        rank.string = "0" + (index + 1);
        rank.node.active = index >= 3;
        let score = item.getChildByName("score").getComponent(cc.Label);
        score.string = this.curdata[index] > 0 ? this.curdata[index].toString() : "--";
    }

    refreshMyRank() {
        this.btnLocal.getChildByName("choose").active = this.isLocal
        this.btnWorld.getChildByName("choose").active = !this.isLocal
        let tip = this.myRank.getChildByName("tip1").getComponent(cc.Label);
        let rank = this.myRank.getChildByName("rank").getComponent(cc.Label);
        let score = this.myRank.getChildByName("score").getComponent(cc.Label);
        if (this.isLocal) {
            tip.string = "本次排名";
            let myRanknum = Helper.getScoreRank(GameData.instance.playScore,  this.curdata)
            rank.string = myRanknum > this.curdata.length ? "--" :  myRanknum.toString();
            score.string = GameData.instance.playScore > 0 ? GameData.instance.playScore.toString() : "--";
        } else {
            tip.string = "我的排名";
            let myRanknum = Helper.getScoreRank(GameData.instance.playScores[0],  this.curdata)
            rank.string = myRanknum > this.curdata.length ? "--" :  myRanknum.toString();
            score.string = GameData.instance.playScores[0] > 0 ? GameData.instance.playScores[0].toString() : "--";

        }

    }

    // update (dt) {}
    public onLocal() {
        this.isLocal = true;
        this.refreshList();
        this.refreshMyRank();

    }

    public onWorld() {
        this.isLocal = false;
        this.refreshList();
        this.refreshMyRank();
    }

    public onClose() {
        this.node.removeFromParent();
        this.node.destroy();
    }
}
